export default {
  moveToScreenCoords( obj, self ) {
    const { linesPlane, camera, $gl } = self
    const hoverInfoEl = document.getElementById('object-hover-info');
    const rect = hoverInfoEl.getBoundingClientRect()
    var x = 2.0 * ((rect.left+10) / $gl.width) - 1
    var y = -2.0 * ((rect.top+10) / $gl.height) + 1

    var mouse3D = new THREE.Vector3( x, y, 0.5 )
    mouse3D.unproject( camera )
    mouse3D.sub( camera.position )
    mouse3D.normalize()

    // console.log(linesPlane)

    var raycaster = new THREE.Raycaster( camera.position, mouse3D )
    var intersects = raycaster.intersectObjects( [ linesPlane ] )

    // console.log(intersects)

    if (intersects.length > 0) {
      obj.position.copy(intersects[0].point)
    }
  },

  findObjectUnderEvent(ev, self) {
    const { $objects, camera, $gl } = self
    const { children } = $objects.blobs
    const { clientX, clientY } = ev

    const x = ( clientX / $gl.width ) * 2 - 1
    const y = -( clientY / $gl.height ) * 2 + 1

    if (!$objects) return

    self.__mouseX = clientX
    self.__mouseY = clientY

    var mouse3D = new THREE.Vector3(x, y, 0.5)

    mouse3D.unproject( camera )
    mouse3D.sub( camera.position )
    mouse3D.normalize()

    var raycaster = new THREE.Raycaster( camera.position, mouse3D )
    var intersects = raycaster.intersectObjects( children.filter((c) => !c.disabled) )

    if ( intersects.length > 0 ) {
      var obj = intersects[ 0 ].object
      return obj
    }
  },

  findToolbarUnderEvent(ev, self) {
    const { toolBar3D, camera, $gl } = self
    const { clientX, clientY } = ev
    const x = ( clientX / $gl.width ) * 2 - 1
    const y = -( clientY / $gl.height ) * 2 + 1

    if (!toolBar3D) return

    self.__mouseX = clientX
    self.__mouseY = clientY

    var mouse3D = new THREE.Vector3(x, y, 0.5)

    mouse3D.unproject( camera )
    mouse3D.sub( camera.position )
    mouse3D.normalize()

    var raycaster = new THREE.Raycaster( camera.position, mouse3D )
    var intersects = raycaster.intersectObjects( toolBar3D.buttons )

    if ( intersects.length > 0 ) {
      var obj = intersects[ 0 ].object
      return obj
    }
  }
}
